/**
 * Program  : HttpUtil.java
 * Author   : qianj
 * Create   : 2012-3-22 下午02:30:15
 *
 * Copyright 2012 by newyulong Technologies Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of newyulong Technologies Ltd.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with newyulong Technologies Ltd.
 *
 */

package com.example.dotavideo.util;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

/**
 * Http请求的帮助文档
 * 
 * @author qianj
 * @version 1.0.0
 * @2012-3-22 下午02:30:15
 */
public class HttpUtil {

	private static final int TIMEOUT = 60 * 1000;// 超时时间 60秒

	public static String requestByGet(String url)
			throws ClientProtocolException, IOException {
		BasicHttpParams httpParams = new BasicHttpParams();  
	    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT);  
	    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT);  
	    HttpClient httpClient = new DefaultHttpClient(httpParams); 
		
		HttpGet request = new HttpGet(url);
		
		request.addHeader("Pragma", "no-cache");
		request.addHeader("Cache-Control", "no-cache");
		request.addHeader("Charset", "UTF-8");
		
		HttpResponse httpResponse = httpClient.execute(request);
		String result = null;
		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
		}
		return result;
		
		
		
//		DefaultHttpClient httpClient = new DefaultHttpClient();
//		HttpGet request = new HttpGet(url);
//		int timeout = 5 * 1000;
//		request.getParams().setParameter("http.socket.timeout",new Integer(timeout));
//		
//		request.addHeader("Pragma", "no-cache");
//		request.addHeader("Cache-Control", "no-cache");
//		request.addHeader("Charset", "UTF-8");
//		
//		// stringEntity = new StringEntity(requData, "UTF-8");
//		// request.setEntity(stringEntity);
//		HttpResponse httpResponse = httpClient.execute(request);
//		String result = null;
//		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//			result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
//		}
//		return result;
	}

	/**
	 * 请求服务器
	 * @param requestUrl
	 * @param requestData
	 * @return
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static String doPost(String requestUrl, String requestData)
			throws ClientProtocolException, IOException {
		
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);
		HttpConnectionParams.setSoTimeout(params, TIMEOUT);
		HttpClient client = new DefaultHttpClient(params);
		
		/* 建立HTTPPost对象 */
		HttpPost httpRequest = new HttpPost(requestUrl);
		String strResult = null;
		
		/* 添加请求参数到请求对象 */
		StringEntity stringEntity = new StringEntity(requestData, "UTF-8");
		httpRequest.setEntity(stringEntity);
		httpRequest.setHeader("Charset", "UTF-8");
		httpRequest.setHeader("Content-Type", "text/xml");
		/* 发送请求并等待响应 */
		// Log.e("httpClient", "httpClient_" + httpClient);
		// Log.e("httpRequest", "httpRequest_"+httpRequest);
		HttpResponse httpResponse = null;
		//try {
			httpResponse = client.execute(httpRequest);
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//			Log.e("网络错误doPost", e.getMessage() + "");
//			return "";
//		}
		if (httpResponse == null) {
			return strResult;
		}
		/* 若状态码为200 ok */
		if (httpResponse.getStatusLine().getStatusCode() == 200) {
			/* 读返回数据 */
			strResult = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
		} else {
			strResult = "Error Response: "
					+ httpResponse.getStatusLine().toString();
		}
		return strResult;
	}

}
